package com.abocode.aichat.app.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 *  Created by guanxf on 2015/12/16.
 */
public class EncryptionUtils {
    private static final   String apiEncryptKey= "ABCD6789";
    private static final   String encryptKey= "zxcv1234";
    private static  boolean debugSwitch=Boolean.valueOf(ConfigUtils.get("auth.debug.switch","false"));

    /***
     * 加密用户密码
     * @param str
     * @return
     */
    public static String toMD5toSHA256(String  str){
       return MD5Utils.toMD5(SHAUtils.toSHA256(str));
    }


    /***
     * token加密
     * @param str
     * @return
     */
    public static String toDESToBASE64(String str) {
        return DESUtils.encrypt(Base64Utils.toBase64(str),encryptKey) ;
    }

    public static String fromDESToBASE64(String str) {
        return Base64Utils.fromBase64(DESUtils.decrypt(str,encryptKey)) ;
    }


    /**
     * 加密请求
     * @param str
     * @return
     */
    public static String toMD5(String  str){
        return MD5Utils.toMD5(str);
    }


    /***
     * 加密Response Body
     * @param object
     * @return
     */
    public static Object toBody(Object  object){
        if(!debugSwitch){
            try {
                return  EncryptionUtils.toBASE64ToDES(new ObjectMapper().writeValueAsString(object));
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
        }
        return  object;
    }

    public static String toBASE64ToDES(String str) {

        return Base64Utils.toBase64(DESUtils.encrypt(str, apiEncryptKey));
    }

    public static String fromBASE64ToDES(String str) {
       return DESUtils.decrypt(Base64Utils.fromBase64(str),apiEncryptKey);
    }

    public  static void main(String[] args){
      /*  System.out.println(EncryptionUtils.toBASE64ToDES("sss+$+1"));
        System.out.println(EncryptionUtils.fromBASE64ToDES(EncryptionUtils.toBASE64ToDES("sss+$+1")));*/
      /* System.out.println(EncryptionUtils.fromBASE64ToDES("em0ydlFTeGVJMVBFTnNKV3ZCYUFCMjAxYjl3aEc0MDBmdWZleEhCK3NBckpQYW5hOUdRVVdOTkFk\n" +
                "                                                            VFUzOG8zdUhpbmw1OFRsd0NhRGZidVM4TTRtZEczbTVRNUtSRWhRdlNRZ08wM2FJUkVSaTRiQ3Q5\n" +
                "                                                            TkNya0NKa2h6dkdIaFA2ME9xWktzWkMvMkV0TFBaRzN1UHh3PT0="));*/
     /*   String data="{\"sex\":1,\"identitytype\":0,\"market\":\"xiaomi\"}";
          String  d="{\"sex\":1,\"identitytype\":0,\"market\":\"xiaomi\"}";
        data="bkdlZ2o5Mm1UUDA3ZEx5RjlFWFoyR0wrbEN5dEw2TERrczZXeFpaRExsMzhHNUNJZ3V1OHQwQnNY\n\rY0hCRzVjRA==";
*/
//        System.out.println( EncryptionUtils.toMD5(EncryptionUtils.toBASE64ToDES("{\"identitytype\":0,\"market\":\"main\",\"sex\":1}")));
       /*  String  str="sss+$+1";
        System.out.println("====>:"+EncryptionUtils.toDESToBASE64(str));
       System.out.println("解密====>:"+EncryptionUtils.fromDESToBASE64(EncryptionUtils.toDESToBASE64(str)));*/
      //  System.out.println(EncryptionUtils.fromDESToBASE64(EncryptionUtils.toBASE64ToDES(str)));
        System.out.println(EncryptionUtils.toMD5toSHA256("60000"+"123456"));

    }

}
